package com.topnav.mc.admin.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import com.topnav.mc.admin.dao.mapper.UserOrganizaitonMapper;
import com.topnav.mc.admin.entity.*;
import com.topnav.mc.admin.entity.bean.UserOrganizaitonBean;
import com.topnav.mc.admin.entity.param.UserOrganizaitonParam;
import com.topnav.mc.admin.service.*;
import com.topnav.mc.base.mc.McServiceImpl;
import com.topnav.mc.exception.ExceptionMsg;
import com.topnav.mc.uitls.ExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.js.lib.exception.CustomException;

@Service
public class UserOrganizaitonServiceImpl extends McServiceImpl<UserOrganizaitonMapper, UserOrganizaiton, UserOrganizaitonBean, UserOrganizaitonParam>
		implements IUserOrganizaitonService {
	@Autowired
	private IUserService userService;
	@Autowired
	private IOrganizationService organizationService;
	@Autowired
	private IRoleService roleService;

	@Override
	public boolean beforeSave(UserOrganizaiton entity){
		if (this.getOne(new QueryWrapper<UserOrganizaiton>().eq("user_id",entity.getUserId()).eq("org_id", entity.getOrgId()), true) != null){
			String[] objs = new String[]{entity.getUserId().toString(),entity.getOrgId().toString()};
			//throw new CustomException(-1,"此机构已在");
			throw ExceptionUtil.commonException(ExceptionMsg.ERROR_PARAM_ORGANIZATION_EXISTS);
		}
		entity.setCreateTime(new Date());
		entity.setUpdateTime(new Date());
		return true;
	}

	@Override
	public boolean beforeUpdate(UserOrganizaiton entity){
		QueryWrapper<UserOrganizaiton> wrapper = new QueryWrapper<UserOrganizaiton>()
				.eq("user_id",entity.getUserId()).eq("org_id", entity.getOrgId()).ne("id", entity.getId());
		if (this.getOne(wrapper, true) != null){
			throw ExceptionUtil.commonException(ExceptionMsg.ERROR_PARAM_ORGANIZATION_EXISTS);
		}
		entity.setUpdateTime(new Date());
		return true;

	}

	@Override
	public boolean beforeDelete(UserOrganizaiton entity){
		return true;
	}

    /**
     * 查询列表
     *
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public List<UserOrganizaiton> list(Wrapper<UserOrganizaiton> queryWrapper) {
		List<UserOrganizaiton> userOrganizaitons;
		if (queryWrapper.getEntity() == null){
			userOrganizaitons = getBaseMapper().selectList(queryWrapper);
		}else{
			QueryWrapper<UserOrganizaiton> wrapper = new QueryWrapper<UserOrganizaiton>();
			if (queryWrapper.getEntity().getId()!=null){
				wrapper.eq("id", queryWrapper.getEntity().getId());
			}

			if (!StringUtils.isEmpty(queryWrapper.getEntity().getUserId())){
				wrapper.eq("user_id", queryWrapper.getEntity().getUserId());
			}
			if (!StringUtils.isEmpty(queryWrapper.getEntity().getOrgId())){
				wrapper.eq("org_id", queryWrapper.getEntity().getOrgId());
			}
			userOrganizaitons = getBaseMapper().selectList(wrapper);
		}

        return userOrganizaitons;
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
	@Override
	public <E extends IPage<UserOrganizaiton>> E page(E page, Wrapper<UserOrganizaiton> queryWrapper) {
		QueryWrapper<UserOrganizaiton> wrapper = new QueryWrapper<UserOrganizaiton>();
		if (queryWrapper.getEntity().getId()!=null){
			wrapper.eq("id", queryWrapper.getEntity().getId());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getUserId())){
			wrapper.eq("user_id", queryWrapper.getEntity().getUserId());
		}
		if (!StringUtils.isEmpty(queryWrapper.getEntity().getOrgId())){
			wrapper.eq("org_id", queryWrapper.getEntity().getOrgId());
		}


        return getBaseMapper().selectPage(page, wrapper);
    }

	/**
	 * @param queryWrapper
	 * @param throwEx
	 * @return
	 */
	@Override
	public UserOrganizaiton getOne(Wrapper<UserOrganizaiton> queryWrapper, boolean throwEx){
		UserOrganizaiton userOrganizaiton = super.getOne(queryWrapper,false);
		return userOrganizaiton;
	}

	/**
	 *
	 * @param id
	 * @return
	 */
	@Override
	public UserOrganizaiton getById(Serializable id) {
		UserOrganizaiton userOrganizaiton = this.getBaseMapper().selectById(id);
		return userOrganizaiton;
	}
}
